﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spider.Business.Stock;
using Spider.Common;
using Spider.Models.Stock;
using System.Reflection;
using System.Threading;
using Spider.Common.Enums;


namespace Spider.Shell
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindSourceUrlGrid();
        }


        private delegate void MyDelegetUI();

        private void BindSourceUrlGrid()
        {
            UrlBusiness business = new UrlBusiness();
            this.gridSourceUrl.DataSource = business.GetSourceUrls();
            this.gridSourceUrl.Refresh();
        }

        #region control actions

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            BindSourceUrlGrid();
        }

        private void btnAddUrl_Click(object sender, EventArgs e)
        {
            AddUrlForm urlForm = new AddUrlForm();
            urlForm.ShowDialog();
        }

        private void gridSourceUrl_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
                return;

            DataGridView view = (DataGridView)sender;
            if(view.Columns[e.ColumnIndex].DataPropertyName == "UrlType")
            {
                FieldInfo fieldInfo = typeof(UrlType).GetField(e.Value.ToString());
                UrlDescAttribute descAttr = (UrlDescAttribute)fieldInfo.GetCustomAttribute(typeof(UrlDescAttribute));
                e.Value = descAttr.Description;
            }

            if (view.Columns[e.ColumnIndex].DataPropertyName == "State")
            {
                int state = Convert.ToInt32(e.Value);
                switch (state)
                { 
                    case (int)ProcessState.Start:
                        e.Value = "未下载";
                        break;
                    case (int)ProcessState.Processing:
                        e.Value = "数据下载中...";
                        break;
                    case (int)ProcessState.Abort:
                        e.Value = "用户中止";
                        break;
                    case (int)ProcessState.Complete:
                        e.Value = "完成";
                        break;
                    default:
                        break;
                }
                
            }
        }

        private void gridSourceUrl_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e == null || !(sender is DataGridView))
                return;

            DataGridView view = (DataGridView)sender;

            if (e.ColumnIndex == view.Columns["clmOperate"].Index)
            {
                var rowData = (SourceUrlModel)view.Rows[e.RowIndex].DataBoundItem;
                SpiderData(rowData);
            }
        }
        
        #endregion


        private void AddLog(string message)
        {
            string strText = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff") + "  " + message;
            if (listLog.InvokeRequired)
            {
                MyDelegetUI d = delegate
                {
                    listLog.Items.Insert(0, strText);
                };
                listLog.Invoke(d);
            }
            else
            {
                listLog.Items.Add(strText);
            }
        }
        private void ShowLog(ProcessState state, string message)
        {
            AddLog(message);

            this.Invoke(new MethodInvoker(delegate
                {
                    if (state == ProcessState.Processing)
                    {
                        progressBar1.Value = (progressBar1.Value >= progressBar1.Maximum) ? 0 : progressBar1.Value + 1;
                    }
                    else
                    {
                        progressBar1.Value = 0;
                    }

                }));

            if(state == ProcessState.Complete || state== ProcessState.Abort)
            {
                this.Invoke(new MethodInvoker(delegate
                    {
                        //同步grid数据 修改button状态
                        BindSourceUrlGrid();
                    }));
            }
            // 写文本日志
        }

        private void SpiderData(SourceUrlModel urlModel)
        {
            ISpider spider = null;

            switch (urlModel.UrlType)
            {
                case UrlType.BasicUrl:
                    spider = new StockCompanyBusiness();
                    break;
                case UrlType.NoticeUrl:
                    spider = new NoticeBusiness();
                    break;
                case UrlType.TradeUrl:

                    break;
                    
                default:
                    break;
            }
            if (spider != null)
            {
                spider.SyncLog += ShowLog;
                spider.SpiderData(urlModel);
            }
        }

        private void btnShowNotice_Click(object sender, EventArgs e)
        {
            NoticeForm form = new NoticeForm();
            form.ShowDialog();
        }

       
    }


}
